package com.yq.cp.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.entity.basics.EquipmentCategory;
import com.yq.cp.entity.basics.EquipmentManagement;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.data.CoefficientYear;
import com.yq.cp.entity.data.NengKongData;
import com.yq.cp.entity.data.NengKongDataDay;
import com.yq.cp.entity.manage.EnergyMac;
import com.yq.cp.entity.manage.EnergyStation;
import com.yq.cp.mapper.basics.EquipmentManagementMapper;
import com.yq.cp.mapper.basics.OrganizationalStructureMapper;
import com.yq.cp.mapper.data.*;
import com.yq.cp.mapper.manage.EnergyMacMapper;
import com.yq.cp.mapper.manage.EnergyStationMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class TimerUtil {

    @Value("${spring.profiles.active}")
    public String active;
    @Resource
    Authorization authorization;
    @Resource
    NengKongDataMapper nengKongDataMapper;
//    @Resource
//    EnergyPlcMapper energyPlcMapper;
    @Resource
    NengKongDataDayMapper nengKongDataDayMapper;
//    @Resource
//    EnergyStationMapper energyStationMapper;
//    @Resource
//    EnergyMacMapper energyMacMapper;
    @Resource
    private NengKongDataDayMapper dataDayMapper;
    @Resource
    CoefficientYearMapper coefficientYearMapper;
    @Resource
    HourPriceMapper hourPriceMapper;
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;
    @Resource
    EquipmentManagementMapper equipmentManagementMapper;


    public static List<NengKongData> list=new ArrayList<>();


    BigDecimal number=new BigDecimal(0);
    BigDecimal number1=new BigDecimal(0);
    BigDecimal number2=new BigDecimal(0);

    List<BigDecimal> scdl=new ArrayList<>();
    Integer cs=0;

    private int  d=0;


    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(fixedRate = 1000*60*100)
    public void timerTotal(){
        //发电功率  100
        // 逆变器总功率(W)
        //ModBus.逆变器1.电表有功功率W.+ModBus逆变器N.电表有功功率W
        String fdgl="ModBus.逆变器.电表有功功率W";
        String fdgl1="ModBus.逆变器";
        String fdgl2="电表有功功率W";


        //发电量 101
        // 日发电量(有功)(kWh)
        //(ModBus.逆变器1.当日发电量01kWh.+ModBus.逆变器N.当日发电量01kWh)*0.1
        String fdl="ModBus.逆变器.当日发电量01kWh";
        String fdl1="ModBus.逆变器";
        String fdl2="当日发电量01kWh";

        //装机容量 102 ----建站时候填写

        //辐射强度 103
        // 辐照强度(W/m2)
        //ModBus.辐照传感器.辐照强度
        String fsqd="ModBus.辐照传感器.辐照强度";
        String fsqd1="ModBus.辐照传感器";
        String fsqd2="辐照强度";

        //有效小时  104
        //(ModBus.逆变器1.当日发电量01kWh.+ModBus.逆变器N.当日发电量01kWh)*0.1/装机规模
        String yxxs="ModBus.逆变器.当日发电量01kWh";
        String yxxs1="ModBus.逆变器";
        String yxxs2="当日发电量01kWh";

        //105-节煤量
        //(ModBus.逆变器1.当日发电量01kWh.+ModBus.逆变器N.当日发电量01kWh)*0.1*0.3007/1000
        String jml="ModBus.逆变器.当日发电量01kWh";
        String jml1="ModBus.逆变器";
        String jml2="当日发电量01kWh";

        //106-减碳排量
        //(ModBus.逆变器1.当日发电量01kWh.+ModBus.逆变器N.当日发电量01kWh)*0.1*0.824/1000
        String jtl="ModBus.逆变器.当日发电量01kWh";
        String jtl1="ModBus.逆变器";
        String jtl2="当日发电量01kWh";

        //130 -上网电量尖


        //上网电量 115
        String swdlz="上网电量总";
        String swdlz1="上网电量";
        String swdlz2="总";
        //上网电量 117
        String xiaowdlz="消网电量总";
        String xiaowdlz1="消网电量";
        String xiaowdlz2="总";
        //下网电量 117
        String xwdlz="下网电量总";
        String xwdlz1="下网电量";
        String xwdlz2="总";


        QueryWrapper<OrganizationalStructure> queryWrapper22=new QueryWrapper<>();
        queryWrapper22.eq("delete_examine",1);
        queryWrapper22.eq("examine",1);
        queryWrapper22.eq("type",6);
        List<OrganizationalStructure> organizationalStructures = organizationalStructureMapper.selectList(queryWrapper22);
        for (int i = 0; i < organizationalStructures.size(); i++) {
            OrganizationalStructure organizationalStructure = organizationalStructures.get(i);
            Long energyId = organizationalStructure.getId();
            if (organizationalStructure==null){
                return;
            }
            String plc = organizationalStructure.getCode();
            Date b = DateTimeUtil.toSimpleDateTime(DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d))+" 00:00:00");
            Date c = DateTimeUtil.toSimpleDateTime(DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d))+" 23:59:59");
            NengKongData nengKongData=new NengKongData();
            //发电功率
            nengKongData = nengKongDataMapper.queryNewestAvg(plc, fdgl1,fdgl2, b, c);
            number1=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
            this.addAvg(number1,100,energyId,b);
            //发电量
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, fdl1, fdl2,b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            BigDecimal qxzxz = dataDayMapper.qxzxz(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)));
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal(number.subtract(qxzxz),101,energyId,b);
            //上网电量  112
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, swdlz1, swdlz2,b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),115);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal(number.subtract(qxzxz),112,energyId,b);
            //消网  113
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, xiaowdlz1, xiaowdlz2,b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),116);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal(number.subtract(qxzxz),113,energyId,b);

            //下网电量  114
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, xwdlz1, xwdlz2,b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),117);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal(number.subtract(qxzxz),114,energyId,b);

            //辐射强度
            nengKongData = nengKongDataMapper.queryNewestAvg(plc, fsqd1,fsqd2, b, c);
            number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
            this.addAvg(number,103,energyId,b);

            //有效小时
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, yxxs1, yxxs2,b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue()).multiply(new BigDecimal(0.1));
            if (organizationalStructure.getInstalledCapacity()!=null && organizationalStructure.getInstalledCapacity().compareTo(new BigDecimal(0))==1){
                number=number.divide(organizationalStructure.getInstalledCapacity(),1);
                this.addTotal(number,104,energyId,b);
            }else {
                this.addTotal(new BigDecimal(24),104,energyId,b);
            }
            QueryWrapper<CoefficientYear> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("year",DateTimeUtil.getYear());
            CoefficientYear coefficientYear = coefficientYearMapper.selectOne(queryWrapper);
            if (coefficientYear==null){
                this.addTotal(new BigDecimal(0),105,energyId,b);
                this.addTotal(new BigDecimal(0),106,energyId,b);
            }else {
                if (coefficientYear.getCoefficient1()!=null){
                    //节煤量 0.3007
                    nengKongData = nengKongDataMapper.queryNewestTotal(plc, jml1, jml2,b, c);
                    number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue()).divide(new BigDecimal(1000)).multiply(coefficientYear.getCoefficient1()).divide(new BigDecimal(1000));
                    this.addTotal(number,105,energyId,b);
                }else {
                    this.addTotal(new BigDecimal(0),105,energyId,b);
                }
                if (coefficientYear.getCoefficient2()!=null){
                    //减碳排量  0.824
                    nengKongData = nengKongDataMapper.queryNewestTotal(plc, jtl1, jtl2,b, c);
                    number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue()).divide(new BigDecimal(1000)).multiply(coefficientYear.getCoefficient2()).divide(new BigDecimal(1000));
                    this.addTotal(number,106,energyId,b);
                }else {
                    this.addTotal(new BigDecimal(0),106,energyId,b);
                }
            }




            //上网电量 ：130-尖 131-峰 132-平 133-谷
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "上网电量", "尖",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),130);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),130,energyId,b);


            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "上网电量", "峰",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),131);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),131,energyId,b);



            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "上网电量", "平",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),132);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),132,energyId,b);


            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "上网电量", "谷",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),133);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),133,energyId,b);
           // 上网电量 ：130-尖 131-峰 132-平 133-谷

            //用户电量 ：140-尖 141-峰 142-平 143-谷
            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "用户电量", "尖",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),140);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),140,energyId,b);


            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "用户电量", "峰",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),141);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),141,energyId,b);



            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "用户电量", "平",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),142);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),142,energyId,b);


            nengKongData = nengKongDataMapper.queryNewestTotal(plc, "用户电量", "谷",b, c);
            number=(nengKongData==null?new BigDecimal(0):nengKongData.getValue());
            qxzxz = dataDayMapper.qxzxz1(energyId, DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), 1+d)),143);
            qxzxz=qxzxz==null?new BigDecimal(0):qxzxz;
            this.addTotal1(number.subtract(qxzxz),143,energyId,b);

            //用户电量 ：140-尖 141-峰 142-平 143-谷

        }


    }
//    @Scheduled(cron = "0 59 * * * ?")
//@Scheduled(fixedRate = 1000*60*1)
public void a(){

        //发电功率  107
        // 逆变器总功率(W)
        //ModBus.逆变器.有功功率W
        String fdgl="ModBus.逆变器.有功功率W";
        String fdgl1="ModBus.逆变器";
        String fdgl2="有功功率W";

        //直流功率

    String zlgl="ModBus.逆变器.有功功率W";
    String zlgl1="ModBus.逆变器";
    String zlgl2="直流功率W";


        //辐射强度 108
        // 辐照强度(W/m2)
        //ModBus.辐照传感器.辐照强度
        String fsqd="ModBus.辐照传感器.辐照强度";
        String fsqd1="ModBus.辐照传感器";
        String fsqd2="辐照强度";

        //ModBus.室外温湿度传感器.温度  110
        String wd="ModBus.室外温湿度传感器.温度";
        String wd1="ModBus.室外温湿度传感器";
        String wd2="温度";

        //ModBus.室外温湿度传感器.湿度  111
        String sd="ModBus.室外温湿度传感器.湿度";
        String sd1="ModBus.室外温湿度传感器";
        String sd2="湿度";

        //发电量 109
        // 日发电量(有功)(kWh)
        //(ModBus.逆变器1.当日发电量01kWh.+ModBus.逆变器N.当日发电量01kWh)*0.1
        String fdl="ModBus.逆变器.当日发电量01kWh";
        String fdl1="ModBus.逆变器";
        String fdl2="当日发电量01kWh";

       //ModBus.逆变器.总发电量kWh
//    String fdl="ModBus.逆变器.总发电量kWh";
//    String fdl1="ModBus.逆变器";
//    String fdl2="总发电量kWh";

       //发电量 109

    //上网电量 115
    String swdlz="上网电量总";
    String swdlz1="上网电量";
    String swdlz2="总";
    //上网电量 116
    String xiaowdlz="消网电量总";
    String xiaowdlz1="消网电量";
    String xiaowdlz2="总";
   //上网电量 117
    String xwdlz="下网电量总";
    String xwdlz1="下网电量";
    String xwdlz2="总";


        //设备状态 132
        //ModBus.逆变器.总直流输入功率W 大于0 则正在工作
        String zt="ModBus.逆变器.总直流输入功率W";
        String zt1="ModBus.逆变器";
        String zt2="总直流输入功率W";

        //负载133
        //ModBus.逆变器.有功功率W
        String fz="ModBus.逆变器.有功功率W";
        String fz1="ModBus.逆变器";
        String fz2="有功功率W";

//        List<EnergyPlc> energyPlcs = energyPlcMapper.selectList(null);
    QueryWrapper<OrganizationalStructure> queryWrapper22=new QueryWrapper<>();
    queryWrapper22.eq("delete_examine",1);
    queryWrapper22.eq("examine",1);
    queryWrapper22.eq("type",6);
    List<OrganizationalStructure> organizationalStructures = organizationalStructureMapper.selectList(queryWrapper22);
    for (int i = 0; i < organizationalStructures.size(); i++) {
            try{
                Integer hour = DateTimeUtil.getHour();
//                if (!"dev".equals(active)){
//                    hour=hour+8;
//                    if (hour>23){
//                        hour=hour-24;
//                    }
//                }

                OrganizationalStructure organizationalStructure = organizationalStructures.get(i);
                String plc = organizationalStructure.getCode();
                Long energyId = organizationalStructure.getId();

                Date b = DateTimeUtil.toSimpleDateTime(DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), d))+" 00:00:00");
                Date c = DateTimeUtil.toSimpleDateTime(DateTimeUtil.getStrDateTime3(DateTimeUtil.getDateBefore(DateTimeUtil.dqsj(), d))+" 23:59:59");
                Date e=DateTimeUtil.dqsj();
                NengKongData nengKongData=new NengKongData();
                //发电功率
//                nengKongData = nengKongDataMapper.queryNewestTotal(plc, fdgl1,fdgl2, b, c);
//                number1=nengKongData==null?new BigDecimal(0):nengKongData.getValue();


//                number1=nengKongDataDayMapper.qxdqgl(energyStation.getId());
//                this.addHour(number1,107,energyId,e,hour,null);
                //直流功率
                number1=nengKongDataDayMapper.qxdqgl1(organizationalStructure.getId());
                this.addHour(number1,120,energyId,e,hour,null);
                //辐照强度
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, fsqd1,fsqd2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,108,energyId,e,hour,null);

                //温度
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, wd1,wd2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,110,energyId,e,hour,null);
                //湿度
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, sd1,sd2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,111,energyId,e,hour,null);

                //发电量
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, fdl1,fdl2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,109,energyId,e,hour,null);

                //上网电量 115
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, swdlz1,swdlz2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,115,energyId,e,hour,null);

                //发电功率  107
                if (scdl.size()<(i+1)){
                    this.addHour1(new BigDecimal(0),107,energyId,e,hour,null);
                    scdl.add(number);
                }else {
                    BigDecimal multiply = number.subtract(scdl.get(i)).multiply(new BigDecimal(18 ));//60/3.33
                    this.addHour1(multiply,107,energyId,e,hour,null);
//                    energyStation.setInstalledKw51(multiply);
//                    energyStationMapper.updateById(energyStation);
                    scdl.set(i,number);
                }

//                if (number.compareTo(new BigDecimal(0))==1){
//
//                }


                //消网网电量 116
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, xiaowdlz1,xiaowdlz2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,116,energyId,e,hour,null);
                //下网电量 117
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, xwdlz1,xwdlz2, b, c);
                number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number,117,energyId,e,hour,null);

                //发电功率
//            this.addHour(number.multiply(new BigDecimal(1.2)),107,energyId,e,hour,null);

                //负载
                nengKongData = nengKongDataMapper.queryNewestTotal(plc, fz1,fz2, b, c);
                number1=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                this.addHour(number1,133,energyId,e,hour,null);

                QueryWrapper<EquipmentManagement> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("site_id",organizationalStructure.getId());
                List<EquipmentManagement> equipmentManagements = equipmentManagementMapper.selectList(queryWrapper);
                for (int j = 0; j < equipmentManagements.size(); j++) {
                    EquipmentManagement equipmentManagement = equipmentManagements.get(j);

                    //功率
                    nengKongData = nengKongDataMapper.queryNewestTotal2(plc, fdgl1,fdgl2, b, c,equipmentManagement.getEquipmentCode());
                    number1=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
//                    equipmentManagement.setRateOfWork(number1);
                    this.addHour(number1,107,energyId,e,hour,equipmentManagement.getId());
                    //状态
                    if (number1.compareTo(new BigDecimal(0))==1 ){
                        equipmentManagement.setOperationState(0);
                    }else {
                        equipmentManagement.setOperationState(1);
                    }
                    //直流功率
                    nengKongData = nengKongDataMapper.queryNewestTotal2(plc, zlgl1,zlgl2, b, c,equipmentManagement.getEquipmentCode());
                    number1=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
//                    equipmentManagement.setRateOfWorkDc(number1);
                    this.addHour(number1,120,energyId,e,hour,equipmentManagement.getId());

                    //温度
                    nengKongData = nengKongDataMapper.queryNewestTotal2(plc, wd1,wd2, b, c,equipmentManagement.getEquipmentCode());
                    number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
                    equipmentManagement.setTemperature(number);
                    //湿度
                    nengKongData = nengKongDataMapper.queryNewestTotal(plc, sd1,sd2, b, c);
                    number=nengKongData==null?new BigDecimal(0):nengKongData.getValue();
//                this.addHour(number,111,energyId,e,hour,energyMac.getId());
                    equipmentManagement.setHumidity(number);



                    equipmentManagementMapper.updateById(equipmentManagement);
                }
            }catch (Exception e){
                e.printStackTrace();
            }


        }
    }
    public void addTotal(BigDecimal value,Integer type,Long energyId,Date d){
        NengKongDataDay nengKongDataDay=new NengKongDataDay();
        nengKongDataDay.setYear(DateTimeUtil.getYear(d));
        nengKongDataDay.setMonth(DateTimeUtil.getMonth(d));
        nengKongDataDay.setDay(DateTimeUtil.getDay(d));
        nengKongDataDay.setValue(value);
        nengKongDataDay.setType(type);
        nengKongDataDay.setEnergyId(energyId);
        nengKongDataDay.setClassifyNumber(1);
        nengKongDataDay.setTime(DateTimeUtil.toSimpleDate(nengKongDataDay.getYear()+"-"+nengKongDataDay.getMonth()+"-"+nengKongDataDay.getDay()));
        nengKongDataDayMapper.insert(nengKongDataDay);
    }

    public void addTotal1(BigDecimal value,Integer type,Long energyId,Date d){
//        * 上网电量 ：130-尖 131-峰 132-平 133-谷
//        * 用户电量 ：140-尖 141-峰 142-平 143-谷
        BigDecimal b=new BigDecimal(0);
        if (type==130){
            b = hourPriceMapper.queryPrice1(1, 1,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==131){
            b = hourPriceMapper.queryPrice1(2, 1,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==132){
            b = hourPriceMapper.queryPrice1(3, 1,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==133){
            b = hourPriceMapper.queryPrice1(4, 1,energyId);
            b=b==null?new BigDecimal(0):b;
        }
        if (type==140){
            b = hourPriceMapper.queryPrice1(1, 2,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==141){
            b = hourPriceMapper.queryPrice1(2, 2,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==142){
            b = hourPriceMapper.queryPrice1(3, 2,energyId);
            b=b==null?new BigDecimal(0):b;
        }else if (type==143){
            b = hourPriceMapper.queryPrice1(4, 2,energyId);
            b=b==null?new BigDecimal(0):b;
        }

        NengKongDataDay nengKongDataDay=new NengKongDataDay();
        nengKongDataDay.setYear(DateTimeUtil.getYear(d));
        nengKongDataDay.setMonth(DateTimeUtil.getMonth(d));
        nengKongDataDay.setDay(DateTimeUtil.getDay(d));
        nengKongDataDay.setValue(value);
        nengKongDataDay.setType(type);
        nengKongDataDay.setEnergyId(energyId);
        nengKongDataDay.setClassifyNumber(1);
        nengKongDataDay.setTime(DateTimeUtil.toSimpleDate(nengKongDataDay.getYear()+"-"+nengKongDataDay.getMonth()+"-"+nengKongDataDay.getDay()));
        nengKongDataDay.setTotalPrices(b.multiply(value));
        nengKongDataDay.setUnitPrice(b);
        nengKongDataDayMapper.insert(nengKongDataDay);
    }

    public void addAvg(BigDecimal value,Integer type,Long energyId,Date d){
        NengKongDataDay nengKongDataDay=new NengKongDataDay();
        nengKongDataDay.setYear(DateTimeUtil.getYear(d));
        nengKongDataDay.setMonth(DateTimeUtil.getMonth(d));
        nengKongDataDay.setDay(DateTimeUtil.getDay(d));
        nengKongDataDay.setValue(value);
        nengKongDataDay.setType(type);
        nengKongDataDay.setEnergyId(energyId);
        nengKongDataDay.setClassifyNumber(2);
        nengKongDataDay.setTime(DateTimeUtil.toSimpleDate(nengKongDataDay.getYear()+"-"+nengKongDataDay.getMonth()+"-"+nengKongDataDay.getDay()));
        nengKongDataDayMapper.insert(nengKongDataDay);
    }
    public void addHour(BigDecimal value,Integer type,Long energyId,Date d,Integer hourNumber,Long macId){
        if (value==null){
            return;
        }
        if (value.compareTo(new BigDecimal(0))==-1){
            value=new BigDecimal(0);
        }
        NengKongDataDay nengKongDataDay=new NengKongDataDay();
        nengKongDataDay.setYear(DateTimeUtil.getYear(d));
        nengKongDataDay.setMonth(DateTimeUtil.getMonth(d));
        nengKongDataDay.setDay(DateTimeUtil.getDay(d));
        nengKongDataDay.setHourNumber(hourNumber);
        String strDateTime4 = DateTimeUtil.getStrDateTime4(d);
        nengKongDataDay.setMinuteNumber(Integer.parseInt(strDateTime4.substring(14,16)));
        nengKongDataDay.setValue(value);
        nengKongDataDay.setType(type);
        nengKongDataDay.setEnergyId(energyId);
        nengKongDataDay.setClassifyNumber(2);
        nengKongDataDay.setMacId(macId);
        nengKongDataDay.setTime(DateTimeUtil.toSimpleDate(nengKongDataDay.getYear()+"-"+nengKongDataDay.getMonth()+"-"+nengKongDataDay.getDay()));
//        nengKongDataDay.setMacCode(macCode);
        nengKongDataDayMapper.insert(nengKongDataDay);
    }
    public void addHour1(BigDecimal value,Integer type,Long energyId,Date d,Integer hourNumber,Long macId){
        if (value==null){
            value=new BigDecimal(0);
        }
        if (value.compareTo(new BigDecimal(0))==-1){
            value=new BigDecimal(0);
        }
        NengKongDataDay nengKongDataDay=new NengKongDataDay();
        nengKongDataDay.setYear(DateTimeUtil.getYear(d));
        nengKongDataDay.setMonth(DateTimeUtil.getMonth(d));
        nengKongDataDay.setDay(DateTimeUtil.getDay(d));
        String strDateTime4 = DateTimeUtil.getStrDateTime4(d);
        int i = Integer.parseInt(strDateTime4.substring(14, 16));
        if (value.compareTo(new BigDecimal(0))==1){
            int i1 = i % 5;
            if (i1!=0){
                i=i-i1;
            }
        }

        nengKongDataDay.setMinuteNumber(i);
        nengKongDataDay.setHourNumber(hourNumber);
        nengKongDataDay.setValue(value);
        nengKongDataDay.setType(type);
        nengKongDataDay.setEnergyId(energyId);
        nengKongDataDay.setClassifyNumber(2);
        nengKongDataDay.setMacId(macId);
        nengKongDataDay.setTime(DateTimeUtil.toSimpleDate(nengKongDataDay.getYear()+"-"+nengKongDataDay.getMonth()+"-"+nengKongDataDay.getDay()));
//        nengKongDataDay.setMacCode(macCode);
        nengKongDataDayMapper.insert(nengKongDataDay);
    }


    @Scheduled(fixedRate = 1000*60)
    public void b(){
        List<NengKongData> l=list;
        list=new ArrayList<>();
        Integer m=0;
        for (int i = 0; i < l.size(); i++) {
            NengKongData nengKongData = l.get(i);
            nengKongDataMapper.insert(nengKongData);
            m++;
        }
        if (m>0){
            this.a();
        }
//        authorization.query();
    }

}
